finishsetup
<script type="text/javascript">

function saveSettings() { 			
 			localStorage["installDone"]='true';
 			
 			if(!import_chrome_history){
				localStorage["lastlocalsync"] = new Date().getTime();
			}else{
				localStorage["lastlocalsync"] = 0;
			}			
			
 			localStorage["username"]=username;
 			localStorage["password"]=password;
			localStorage["devicename"]=devicename;
			localStorage["server_url"]=server_url;
			localStorage["replace_chrome_history"]=replace_chrome_history;
			localStorage["wanted_devices"]=wanted_devices;
			localStorage["import_chrome_history"]=import_chrome_history;
			localStorage["sync_local_to_server"]=sync_local_to_server;
			localStorage["sync_server_to_local"]=sync_server_to_local;
			localStorage["lastDevicesSyncTimes"]="{\""+devicename+"\":0}";
			
			var obj=$.parseJSON(localStorage["lastDevicesSyncTimes"]);
			for (var i=0; i < devices.length; i++) {			  
			  obj[devices[i]]=0;
			};			
			obj[devicename]=0;
			localStorage["lastDevicesSyncTimes"]=JSON.stringify(obj);			
			
			localStorage["lastLocal2ServSync"]=0;	
			bg.insertdevice(devicename);	
}

function finishSetup() {
	if (localStorage["import_chrome_history"] === "true") {
		//bg.dirtyPollLastWeek();
		//TODO: cause work dirtypoll lastLocal2ServSync cant be getted properly
	}	
	
	//first poll for local histoy sync
	bg.initSynchronisation();
}

//login account
function login_account() {
    $('#info').fadeOut(0);
    $('#info').fadeIn(300).html('Contacting Server for Login...');
    try {
        $.post(server_url, {
            c: 4,
            u: username,
            p: bg.getHash(password),
            d: devicename
        }, function (data) {
            try {
            	console.log("login_account - 1");
                var json_obj = $.parseJSON(data);
                switch (json_obj.code) {
                case 0:
                    //success
                    $('#info').fadeOut(0);
                    $('#info').fadeIn(300).html('Setup was succesfully finished.');
                    $('#content').fadeOut(300);
                    $('#header').fadeOut(300);
					console.log("login_account - 2");
                    saveSettings();
                    finishSetup();							
                    break;

                case 1:
                    //general error
                    $('#info').fadeOut(0);
                    $('#info').fadeIn(300).html('Unknown Error, setup couldnt be finished.');
                    break;

                case 2:
                    //general error
                    $('#info').fadeOut(0);
                    $('#info').fadeIn(300).html('Account couldnt be created, setup couldnt be finished.');
                    break;

                default:
                    break;
                }
            } catch (err) {
                //general error
                $('#info').fadeOut(0);
                $('#info').fadeIn(300).html('Very Unknown Error, setup couldnt be finished: ' + err);
            }
        });
    } catch (err) {
        //general error
        $('#info').fadeOut(0);
        $('#info').fadeIn(300).html('HTTP Post Error: ' + err);
    }
}

	function readWantedDevices() {
		var result = [];
		$('.owndevices:input[type=checkbox]:checked').each(function(index, value) {
			result.push(value.name);
		});
		
		result.push(devicename);
		return JSON.stringify(result);
		
	}

	function insertDevicesIntoTable() {
		var devices_table_string = "";

		if (devices.length > 0) {
			devices_table_string = '<tr><td>Devices:</td></tr>';
		}

		var i;
		for (i = 0; i < devices.length; i++) {
			devices_table_string += '<tr><td><input class="owndevices" type="checkbox" name="' + devices[i] +'" id="device_' + devices[i] + '" checked="true">' + devices[i] + '</td></tr>';
		}

		$("#devices").html(devices_table_string);
	}

	function checkboxClicked(event) {
		if (!$('#sync_serv_to_local').prop("checked")) {
			$('#devices input').attr('disabled', true);
		} else {
			$('#devices input').attr('disabled', false);
		}
	}
	
	$('#button_finish').click(function() {
		replace_chrome_history = "true";
		wanted_devices = readWantedDevices();
		import_chrome_history = $('#import:checked').val();
		sync_local_to_server = $('#sync_local_to_serv:checked').val();
		sync_server_to_local = $('#sync_serv_to_local:checked').val();
		login_account();
	});

	$('#sync_serv_to_local').bind('click', checkboxClicked);
	insertDevicesIntoTable();
</script>

<div id="content0">	
	
	<table>	
	<tr><td>
	General Options:
	</td></tr>
	<tr><td>
	<input type=checkbox id="import" value="true" checked="checked"> Import Chrome History until now
	</td></tr>
	<tr><td>
	<br />
	</td></tr>
	<tr><td>
	Synchronisation Options:
	</td></tr>
	<tr><td>
	<input type=checkbox id="sync_local_to_serv" value="true" checked="checked"> Sync Local-History Changes to Server
	</td></tr>
	<tr><td>
	<input type=checkbox id="sync_serv_to_local" value="true" checked="checked"> Sync Server-History Changes to Local
	</td></tr>
	<tr><td>
	<table id="devices" style="padding-left: 15px;">
	</table>
	</td></tr>
	<tr><td>
	<input type="button" id="button_finish" value="Finish Setup"></button>
	</td></tr>
	</table>	
</div>